package com.cdh.base;

import com.cdh.utils.result.Restful;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody //返回数据
    public Restful error(Exception e){
        e.printStackTrace();
        return Restful.error().message("执行了全局异常处理！");
    }


    // 特定异常处理
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public Restful error(ArithmeticException e){
        e.printStackTrace();
        return Restful.error().message("执行了ArithmeticException异常处理！");
    }

    // 自定义异常处理
    @ExceptionHandler(DIYException.class)
    @ResponseBody
    public Restful error(DIYException e){
        log.error(e.getMessage());
        e.printStackTrace();
        return Restful.error().code(e.getCode()).message(e.getMsg());
    }
}
